/**
 * 文件名称: main.c
 * 摘    要: linux应用程序下操作硬件rtc测试
 * 来    源: 自我总结
 *
 * 当前版本: 1.0
 * 作    者: huenrong
 * 完成日期: 2019年06月13日
 **/


#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>

#include "hardware_rtc.h"

int main(void)
{
    int ret = -1;
    struct rtc_time rtc_tm = {0};

    printf("read time start...\n");
    ret = hardware_rtc_read_date_time(RTC_DEV_NAME, &rtc_tm);
    if (-1 == ret)
    {
        printf("rtc read error!\n");
    }

    printf("readtime: %d-%d-%d, %02d:%02d:%02d\n", rtc_tm.tm_year + 1900, rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
    printf("read time end...\n\n");

    printf("set time start...\n");
    rtc_tm.tm_year = 2017 - 1900;
    rtc_tm.tm_mon = 2 - 1;
    rtc_tm.tm_mday = 20;
    rtc_tm.tm_hour = 21;
    rtc_tm.tm_min = 0;
    rtc_tm.tm_sec = 0;
    ret = hardware_rtc_set_date_time(RTC_DEV_NAME, rtc_tm, 1);        // 设置硬件时间并同步系统时间
    if (-1 == ret)
    {
        printf("rtc set error!\n");
    }

    printf("settime: %d-%d-%d, %02d:%02d:%02d\n", rtc_tm.tm_year + 1900, rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
    printf("set time end...\n\n");

    sleep(1);

    printf("read time start...\n");
    for (int i = 0; i < 10; i++)
    {
        ret = hardware_rtc_read_date_time(RTC_DEV_NAME, &rtc_tm);
        if (-1 == ret)
        {
            printf("rtc read error!\n");
        }

        printf("readtime: %d-%d-%d, %02d:%02d:%02d\n", rtc_tm.tm_year + 1900, rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);

        sleep(1);
    }
    printf("read time end...\n");


    return 0;
}